package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemUserSimpleBinding;
import com.jwdf.uda.ui.adapter.item.UserSimpleItem;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by Administrator on 2016/5/16.
 */
public class UserSimpleAdapter extends AbsAdapter<UserSimpleItem> {
    private OnUserFollowTypeListener mListener;
    private boolean mIsCurrentUser = false;

    public UserSimpleAdapter(Context context) {
        super(context);
    }

    public void setOnUserFollowTypeListener(OnUserFollowTypeListener listener) {
        this.mListener = listener;
    }

    public void setCurrentUser(boolean isCurrentUser) {
        this.mIsCurrentUser = isCurrentUser;
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        return new UserSimpleViewHolder(mInflater.inflate(R.layout.item_user_simple, parent, false));
    }
    private void onUserFollowTypeClick(UserSimpleItem item) {
        PbBean.PbUserSimple userSimple = item.userSimple;
        PbBean.PbSignUserFollowType type = item.type;
//        Drawable.ConstantState state = imageView.getDrawable().getCurrent().getConstantState();
        String userId = userSimple.getUserId();
//                Drawable.ConstantState ONE_WAY_STATE = imageView.getContext().getResources().getDrawable(R.mipmap.ic_oneway, null).getConstantState();
//                Drawable.ConstantState NONE_STATE = imageView.getContext().getResources().getDrawable(R.mipmap.ic_add_follow, null).getConstantState();
//                Drawable.ConstantState BOTH_FOLLOW_STATE = imageView.getContext().getResources().getDrawable(R.mipmap.ic_follow, null).getConstantState();
//                if (ONE_WAY_STATE.equals(state) || BOTH_FOLLOW_STATE.equals(state)) {
//                    if (mListener != null && NETUtils.isNetworkConnected(imageView.getContext())) {
//                        imageView.setImageResource(R.mipmap.ic_add_follow);
//                        mListener.onCancelFollow(userId);
//                    } else {
//                        ToastUtils.show(imageView.getContext(), "网络不给力，请检查网络设置");
//                    }
//                } else if (NONE_STATE.equals(state)) {
//                    if (mListener != null && NETUtils.isNetworkConnected(imageView.getContext())) {
//                        if (type.ordinal() == PbBean.PbSignUserFollowType.USER_FOLLOW_TYPE_NONE.ordinal()) {
//                            imageView.setImageResource(R.mipmap.ic_oneway);
//                        } else if (type.ordinal() == PbBean.PbSignUserFollowType.USER_FOLLOW_TYPE_FANS.ordinal()) {
//                            imageView.setImageResource(R.mipmap.ic_follow);
//                        }
//                        mListener.onAddFollow(userId);
//                    } else {
//                        ToastUtils.show(imageView.getContext(), "网络不给力，请检查网络设置");
//                    }
//                }
        switch(type) {
            case USER_FOLLOW_TYPE_FOLLOW:
            case USER_FOLLOW_TYPE_BOTH:
                if(mListener != null) {
                    mListener.onCancelFollow(userId);
                }
                break;
            case USER_FOLLOW_TYPE_FANS:
            case USER_FOLLOW_TYPE_NONE:
                if(mListener != null) {
                    mListener.onAddFollow(userId);
                }
                break;
        }
    }

    public interface OnUserFollowTypeListener {
        void onAddFollow(String userId);

        void onCancelFollow(String userId);
    }

    private class UserSimpleViewHolder extends AbsViewHolder<UserSimpleItem> {
        private ItemUserSimpleBinding mBinding;

        public UserSimpleViewHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(final UserSimpleItem item) {
            final PbBean.PbUserSimple userSimple = item.userSimple;

            boolean isOwner = TextUtils.equals(userSimple.getUserId(), ConfigUtils.getUserId(mBinding.getRoot().getContext()));
            mBinding.setIsOwner(isOwner);
            mBinding.setIsDesigner(userSimple.getIsDesigner());
            ImageLoader.getInstance().displayImage(userSimple.getAvatar().getUrl(), mBinding.ivHeader);
            mBinding.tvNickName.setText(userSimple.getNickname());
//            mBinding.tvIntroduction.setText(userSimple.getin);
            mBinding.tvIntroduction.setVisibility(TextUtils.isEmpty(StringUtils.filterNullAndTrim(userSimple.getIntroduction())) ? View.GONE:View.VISIBLE);
            mBinding.tvIntroduction.setText(userSimple.getIntroduction());
            mBinding.ivFollow.setVisibility(View.INVISIBLE);
//            if (mIsCurrentUser) {
                PbBean.PbSignUserFollowType type = item.type;
                if (type.ordinal() == PbBean.PbSignUserFollowType.USER_FOLLOW_TYPE_FOLLOW.ordinal()) {
                    mBinding.ivFollow.setImageResource(R.mipmap.ic_oneway);
                } else if (type.ordinal() == PbBean.PbSignUserFollowType.USER_FOLLOW_TYPE_NONE.ordinal() || type.ordinal() == PbBean.PbSignUserFollowType.USER_FOLLOW_TYPE_FANS.ordinal()) {
                    mBinding.ivFollow.setImageResource(R.mipmap.ic_add_follow);
                } else {
                    mBinding.ivFollow.setImageResource(R.mipmap.ic_oneway);
                }
                mBinding.ivFollow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onUserFollowTypeClick(item);
                    }
                });
//            } else {
//                //
//                mBinding.setIsOwner(true);
//                mBinding.ivFollow.setImageResource(R.mipmap.ic_add_follow);
//                mBinding.ivFollow.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        String userId = userSimple.getUserId();
//                        if (mListener != null) {
//                            mListener.onAddFollow(userId);
//                        }
//                    }
//                });
//            }

        }
    }
}
